package com.biye.api.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.base.common.edu.entity.Department;
import com.base.common.edu.entity.Staff;
import com.base.common.edu.mapper.DepartmentMapper;
import com.base.common.edu.mapper.StaffMapper;
import com.base.common.edu.util.Page;
import com.base.common.httpStatusCode.enumDefine.StatusCode;
import com.biye.api.service.DepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private StaffMapper staffMapper;

    @Override
    public JSONObject saveDept(Department department) {
        JSONObject json = new JSONObject();
        String msg = "新增部门失败，请稍后再试！";
        int statusCode = StatusCode.Failure._statusCode;
        // 首先是否已经存在，若存在则不重复添加
        int count = departmentMapper.existDeptJudge(department.getDeptName());
        if ( count > 0){
            msg = "新增部门失败，部门已存在！";
        }else if( count == 0 ){
            int effectLine = this.departmentMapper.saveDept(department);
            if( effectLine > 0 ) {
                msg = "新增部门成功";
                statusCode = StatusCode.Success._statusCode;
            }
        }
        json.put("msg", msg);
        json.put("statusCode", statusCode);
        return json;
    }

    @Override
    public JSONObject queryListOfDeptName() {
        JSONObject result = new JSONObject();
        String msg = "暂无数据";
        int statusCode = StatusCode.NoResult._statusCode;
        List<Department> deptList = this.departmentMapper.queryListOfDeptName();
        if( deptList.size() > 0 ) {
            msg = "获取数据成功";
            statusCode = StatusCode.Success._statusCode;
            JSONArray json1 = new JSONArray();
            Map<String, Object> map;
            for(Department dept:deptList){
                map = new HashMap<>();
                map.put("deptId", dept.getDeptId());
                map.put("deptName", dept.getDeptName());
                json1.add(map);
            }
            result.put("deptList", json1);
        }
        result.put("msg", msg);
        result.put("statusCode", statusCode);
        return result;
    }

    @Override
    public JSONObject updateDeptInfo(Map<String, Object> requestMap) {
        JSONObject result = new JSONObject();
        String msg = "无任何数据被更新";
        int statusCode = StatusCode.Failure._statusCode;
        // 检查请求参数的合法性
        if (
                !requestMap.containsKey("deptId") ||
                        !requestMap.containsKey("deptName") ||
                        !requestMap.containsKey("staff_no")
        ){
            result.put("msg", "请求参数有误");
            result.put("statusCode", statusCode);
            return result;
        }
        // 获取参数
        String deptId = (String) requestMap.get("deptId");
        String deptName = (String) requestMap.get("deptName");
        deptName = "".equals(deptName)? null : deptName;
        String staff_no = (String) requestMap.get("staff_no");
        staff_no = "".equals(staff_no)? null : staff_no;
        // 判断
        Department department = departmentMapper.queryDepartmentById(deptId);
        if ( department != null ){
            if ( deptName != null && deptName.equals(department.getDeptName()) )
                department.setDeptName(deptName);
            if (staff_no != null)
            {
                String staffIdByTno = staffMapper.getStaffIdByTno(staff_no);
                if ( staffIdByTno != null ){
                    Staff sta = new Staff();
                    sta.setStaffId(staffIdByTno);
                    department.setStaff(sta);
                }
            }
            // 修改数据
            int i = departmentMapper.updateDeptInfo(department);
            if ( i > 0 ){
                msg = "数据更新成功";
                statusCode = StatusCode.Success._statusCode;
            }
        }
        result.put("msg", msg);
        result.put("statusCode", statusCode);
        return result;
    }

    @Override
    public JSONObject deleteDept(Map<String, Object> map) {
        JSONObject result = new JSONObject();
        String msg = "操作失败，请稍后再试";
        int statusCode = StatusCode.Failure._statusCode;
        boolean exist = map.containsKey("deptId");
        if ( exist ){
            statusCode = StatusCode.Success._statusCode;
            String deptId = (String) map.get("deptId") ;
            System.out.println(deptId);
            int affectLine = departmentMapper.deleteDept(deptId);
            if( affectLine > 0 ) {
                msg = "操作成功，数据已被删除";
            }else{
                statusCode = StatusCode.NoResult._statusCode;
                msg = "操作已执行，无任何数据被删除";
            }
        }else {
            msg = "操作失败，参数不正确";
        }
        result.put("msg", msg);
        result.put("statusCode", statusCode);
        return result;
    }

    @Override
    public JSONObject queryDept_Paging(Map<String, Object> request__map) {
        JSONObject json = new JSONObject();
        String msg = "未搜索到任何数据";
        int statusCode = StatusCode.Failure._statusCode;
        // 请求参数合法性校验
        if ( !request__map.containsKey("pageSize") || !request__map.containsKey("pageNum") ){
            json.put("msg", "请求参数有误请检查");
            json.put("statusCode", statusCode);
            return json;
        }
        // 获得请求参数
        int pageNum = (int) request__map.get("pageNum");
        int pageSize = (int) request__map.get("pageSize");
        // 若有模糊搜索，则取出条件
        String dept__id = (String) request__map.get("deptId");
        boolean choose_way = false;
        if ( dept__id != null && !"".equals(dept__id) )
            choose_way = true;
        // 设置分页查询，它会automatic在后面的所执行的select语句外边嵌套分页逻辑
        PageHelper.startPage(pageNum, pageSize);
        List<Department> dept_list = null;
        if ( choose_way )
            dept_list = departmentMapper.queryListOfDeptById(dept__id);
        if ( ! choose_way )
            dept_list = departmentMapper.queryListOfDeptById(null);
        if ( dept_list. size() > 0 ){
            PageInfo<Department> pageInfo = new PageInfo<>(dept_list);
            Page<Map<String, Object>> p = new Page<Map<String, Object>>(pageNum, pageSize, (int) pageInfo.getTotal());
            List<Map<String, Object>> sa_list = new ArrayList<>();
            Map<String, Object> map_obj;
            for ( Department dept : dept_list ) {
                map_obj = new HashMap<>();
                map_obj.put("deptId", dept.getDeptId());
                map_obj.put("deptName", dept.getDeptName());
                if ( dept.getStaff() != null ){
                    map_obj.put("chairmen_name", dept.getStaff().getName());
                    map_obj.put("chairman_tno", dept.getStaff().getTno());
                }else{
                    map_obj.put("chairmen_name", "");
                    map_obj.put("chairman_tno", "");
                }
                sa_list.add(map_obj);
            }
            p.setList(sa_list);
            msg = "数据查询成功";
            statusCode = StatusCode.Success._statusCode;
            json.put("page", p);
        }
        json.put("msg", msg);
        json.put("statusCode", statusCode);
        return json;
    }

}
